package com.mickey.service.Imp;

import com.mickey.dao.MiaoshaUserDao;
import com.mickey.domain.MiaoshaUser;
import com.mickey.exception.GlobleException;
import com.mickey.redis.MiaoshaUersKey;
import com.mickey.redis.RedisService;
import com.mickey.result.CodeMsg;
import com.mickey.service.MiaoshaUserService;
import com.mickey.util.MD5Util;
import com.mickey.util.UUIDUtil;
import com.mickey.vo.LoginVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

@Service
public class MiaoshaUserUserServiceImp implements MiaoshaUserService {

    public static final String COOKIE_NAME_TOKEN = "token";

    @Autowired
    private MiaoshaUserDao miaoshaUserDao;
    @Autowired
    private RedisService redisService;

    @Override
    public CodeMsg login(LoginVO loginVO,HttpServletResponse response) {
        if(loginVO == null){
            throw new GlobleException(CodeMsg.SERVICE_ERROR);
        }
        String password = loginVO.getPassword();
        String mobile = loginVO.getMobile();
        if (StringUtils.isEmpty(password)){
            throw new GlobleException(CodeMsg.PASSWORD_NULL);
        }
        if(StringUtils.isEmpty(mobile)){
            throw new GlobleException(CodeMsg.MOBILE_NULL);
        }

        MiaoshaUser user = getById(Long.valueOf(mobile));
        if(user == null){
            throw new GlobleException(CodeMsg.MOBILE_EMPTY);
        }else{
            String calcPass = MD5Util.formPassToDBPass(password,user.getSalt());
            if(!calcPass.equalsIgnoreCase(user.getPassword())){
                throw new GlobleException(CodeMsg.MOBILE_ERR);
            }
        }
        String token = UUIDUtil.uuid();
        //生成token
        addCookie(user,token,response);
        return CodeMsg.SUCESS;
    }

    @Override
    public MiaoshaUser getById(long id) {
        MiaoshaUser user = redisService.get(MiaoshaUersKey.getById,"" +id,MiaoshaUser.class);
        if (user!=null){
            return user;
        }
        user = miaoshaUserDao.getById(id);
        if(user!=null){
            redisService.set(MiaoshaUersKey.getById,"" + id,user);
        }
        return user;
    }

    @Override
    public MiaoshaUser getByToken(String token,HttpServletResponse response) {
        if(StringUtils.isEmpty(token)){
            return null;
        }
        MiaoshaUser user = redisService.get(MiaoshaUersKey.token,token,MiaoshaUser.class);
        //延长有效期
        if (user!=null){
            addCookie(user,token,response);
        }
        return user;
    }


    //生成cookie
    private void addCookie(MiaoshaUser user,String token,HttpServletResponse response){
        redisService.set(MiaoshaUersKey.token,token,user);
        Cookie cookie = new Cookie(COOKIE_NAME_TOKEN,token);
        cookie.setMaxAge(MiaoshaUersKey.token.expireSeconds());
        cookie.setPath("/");
        response.addCookie(cookie);
    }


}
